import { _decorator, Component, EventKeyboard, input, Input, KeyCode, Label,  Vec2, Vec3 } from 'cc';
import { PlayerData } from '../api/pb/sync_pb';
import { PlayerLocalData } from '../PlayerLocalData';
const { ccclass, property } = _decorator;

@ccclass('Player')
export class Player extends Component {

    public isLocal: boolean = true;
    speed: number = 60;
    nameLabel:Label
    debug_pos:Label
    direction:Vec2 = new Vec2(0,0)

    public playerData:PlayerLocalData


    start() {
        this.nameLabel = this.node.getChildByName("name").getComponent(Label);
        this.nameLabel.string = `${this.playerData.id}`
        this.debug_pos = this.node.getChildByName("debug_pos").getComponent(Label);
        this.node.setPosition(new Vec3(this.playerData.x,this.playerData.y))


        input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        input.on(Input.EventType.KEY_UP, this.onKeyUp, this);

        this.setPos()
    }

    init(playerData: PlayerData, local: boolean) {
        this.isLocal = local
        this.playerData = new PlayerLocalData();

        this.playerData.id = playerData.id as unknown as number
        this.playerData.x = playerData.x
        this.playerData.y = playerData.y

    }

    private onKeyDown(event: EventKeyboard) {
        if(!this.isLocal){
            return
        }
        let old = new Vec2(this.direction.x,this.direction.y)
        switch(event.keyCode){
            case KeyCode.KEY_W:
                this.direction.y = 1
                break;
            case KeyCode.KEY_S:
                this.direction.y = -1
                break;
            case KeyCode.KEY_A:
                this.direction.x = -1
                break;
            case KeyCode.KEY_D:
                this.direction.x = 1
                break;
        }
        if(old.x!=this.direction.x || old.y!=this.direction.y){
            //TODO 发送给服务器
        }
    }
    private onKeyUp(event: EventKeyboard) {
        if(!this.isLocal){
            return
        }
        switch(event.keyCode){
            case KeyCode.KEY_W:
                if(this.direction.y == 1){
                    this.direction.y = 0
                }
                break;
            case KeyCode.KEY_S:
                if(this.direction.y == -1){
                    this.direction.y = 0
                }
                break;
            case KeyCode.KEY_A:
                if(this.direction.x == -1){
                    this.direction.x = 0
                }
                break;
            case KeyCode.KEY_D:
                if(this.direction.x == 1){
                    this.direction.x = 0
                }
                break;
        }
    }

    update(deltaTime: number) {
        if(!this.isLocal){
            return
        }
        if(this.direction.x == 0 && this.direction.y == 0){
            return
        }
        let newPos = this.node.position.add3f(this.direction.x * this.speed * deltaTime,this.direction.y * this.speed * deltaTime, 0 )
        this.node.setPosition(newPos)
        this.setPos()
    }
    setPos(){
        this.debug_pos.string = `${this.node.position.x.toFixed(2)} , ${this.node.position.y.toFixed(2)}`
    }
    protected onDestroy(): void {
        input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        input.off(Input.EventType.KEY_UP, this.onKeyUp, this);
    }
}


